package alo.p17;

import alo.p6.BinaryTreeNode;
import alo.p7.Heap;

/**
 * @ClassName HuffmanCodingAlgorithm
 * @Description 赫夫曼编码
 * @Author Fanxc
 * @DATE 2021/3/14 15:04
 * @Version 1.0
 */
public class HuffmanCodingAlgorithm {
    Heap huffmanCodingAlgorithm(int[] A, int n){
        Heap heap=new Heap(n,0);
        heap.BuildHeap(heap,A,n);
        BinaryTreeNode temp;
        for (int i = 1; i < n; i++) {
            temp=new BinaryTreeNode();
            temp.setLeft(new BinaryTreeNode(heap.DeleteMax()));
            temp.setRight(new BinaryTreeNode(heap.DeleteMax()));
            temp.setData(temp.getLeft().getData()+temp.getRight().getData());
            heap.Insert(temp.getData());
        }
        return heap;
    }
}
